import tkinter as tk

class ScrollFrame(tk.Frame):
    def __init__(self,*argv,**kwords):
        super().__init__(*argv,**kwords)
        self._design()
    def _design(self):
        self.canvas = tk.Canvas(self,bg="red")
        self.scro = tk.Scrollbar(self)
        self.frame = tk.Frame(self.canvas)
        self.canvas.pack(side='left',fill='x')

        self.scro.pack(side='right',fill='y')
        
        self.scro.config(command=self.canvas.yview)
        self.frame.pack(fill='x')

        self.canvas.configure(yscrollcommand=self.scro.set, scrollregion=self.canvas.bbox("all"))
        self.canvas.create_window((0,0),window=self.frame,anchor="nw")

        
        
        # button1 = tk.Button(master=self.frame,text='1').grid(row=1,column=0)
        # button2 = tk.Button(master=self.frame,text='2').grid(row=2,column=0)
        # button3 = tk.Button(master=self.frame,text='3').grid(row=3,column=0)
        # button4 = tk.Button(master=self.frame,text='4').grid(row=4,column=0)
        # button5 = tk.Button(master=self.frame,text='5').grid(row=5,column=0)
        # button6 = tk.Button(master=self.frame,text='x').grid(row=6,column=0)
        # button7 = tk.Button(master=self.frame,text='y').grid(row=7,column=0)
        # button8 = tk.Button(master=self.frame,text='z').grid(row=8,column=0)
        # button9 = tk.Button(master=self.frame,text='@').grid(row=9,column=0)
        # button10= tk.Button(master=self.frame,text='#').grid(row=10,column=0)
        # 更新Frame大小，不然没有滚动效果
        
    def m_update(self):
        self.frame.pack(fill='x')

        self.canvas.update()
        self.canvas.configure(yscrollcommand=self.scro.set, scrollregion=self.canvas.bbox("all"))
        ...

    # def add(self,wgt):
    #     wgt.pack(fill=tk.X)


if __name__=="__main__":
    win = tk.Tk()
    win.geometry("300x200")
    lz = ScrollFrame(win)
    lz.grid(column=0,row=0,sticky="wnse")
    for idx in range(20):
        if idx%2:
            tk.Button(lz.frame,text=str(idx)).pack(fill=tk.X)
        else:
            tk.Label(lz.frame,text=str(idx)).pack(fill=tk.X)
    lz.m_update()
    win.columnconfigure(0,weight=1)
    win.rowconfigure(0,weight=1)
    win.mainloop()


    